﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Generic.Business.Validation
{
    public abstract class BusinessValidation
    {
        protected bool InstanceThrowError { get; private set; }

        public List<string> ValidationResults { get; set; }

        public BusinessValidation(bool instanceThrowError)
        {
            this.InstanceThrowError = instanceThrowError;

            this.ValidationResults = new List<string>();
        }

        public BusinessValidation()
        {
            this.ValidationResults = new List<string>();
        }

        public virtual void ThrowBusinessValidationError()
        {
            try
            {
                if (this.ValidationResults.Count == 0)
                    return;

                string validationMessage = "";

                foreach (string vr in this.ValidationResults)
                {
                    if (validationMessage.Length > 0)
                        validationMessage += "\n";

                    validationMessage += vr;
                }

                throw new Exception(validationMessage);
            }
            catch (Exception) { throw; }
        }

        public virtual void AddValidationResultNotValid(string errorMessage)
        {
            try
            {
                this.ValidationResults.Add(errorMessage);
            }
            catch (Exception) { throw; }
        }

        public virtual void ClearValidationResults()
        {
            try
            {
                this.ValidationResults = new List<string>();
            }
            catch (Exception) { throw; }
        }

    }
}
